package com.example.fabric;

import com.example.fabric.config.FabricConfig;
import com.example.fabric.config.OrgConfig;
import org.hyperledger.fabric.sdk.*;
import org.hyperledger.fabric.sdk.exception.InvalidArgumentException;
import org.hyperledger.fabric.sdk.security.CryptoSuite;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * \* Created with IntelliJ IDEA.
 * \* User: 86182
 * \* Date: 2020/4/12
 * \* Time: 18:34
 * \* To change this template use File | Settings | File Templates.
 * \* Description:
 * \
 */
@Component
public class InitFabricClient implements CommandLineRunner {
    @Autowired
    private FabricConfig fabricConfig;

    private HFClient hfclient;

    private Channel channel;

    private User user;

    public HFClient getHfclient() {
        return hfclient;
    }

    public Channel getChannel() {
        return channel;
    }

    public User getUser() {
        return user;
    }

    @Override
    public void run(String... args) throws Exception {
        HFClient hfclientTmp = HFClient.createNewInstance();
        CryptoSuite cryptoSuite = CryptoSuite.Factory.getCryptoSuite();
        hfclientTmp.setCryptoSuite(cryptoSuite);

        OrgConfig org1 = fabricConfig.getOrgList().get(0);
        OrgConfig org2 = fabricConfig.getOrgList().get(1);
        OrgConfig userOrg = org1;

        //设置用户,name任意
        User userTmp = FabricDemo.getFabricUser4Local(userOrg.getName(), userOrg.getOrg(), userOrg.getOrgId(), userOrg.getCertFile(), userOrg.getSkFile());
        hfclientTmp.setUserContext(userTmp);

        //创建通道的客户端代理
        Channel channelTmp = hfclientTmp.newChannel(fabricConfig.getChannelName());

        fabricConfig.getOrdererList().forEach(orderAddr -> {
            try {
                Orderer orderer = hfclientTmp.newOrderer(orderAddr.getName(), orderAddr.getUrl());
                channelTmp.addOrderer(orderer);
            } catch (InvalidArgumentException e) {
                e.printStackTrace();
            }
        });

        Map<String, List<Peer>> peers = new HashMap<>();
        List<Peer> peers1 = new ArrayList<>();
        org1.getPeerList().forEach(peerAddr -> {
            try {
                Peer peer = hfclientTmp.newPeer(peerAddr.getName(), peerAddr.getUrl());
                peers1.add(peer);
                channelTmp.addPeer(peer);//添加背书节点
            } catch (InvalidArgumentException e) {
                e.printStackTrace();
            }
        });
        peers.put(org1.getName(), peers1);

        List<Peer> peers2 = new ArrayList<>();
        org2.getPeerList().forEach(peerAddr -> {
            try {
                Peer peer = hfclientTmp.newPeer(peerAddr.getName(), peerAddr.getUrl());
                peers2.add(peer);
                channelTmp.addPeer(peer);//添加背书节点
            } catch (InvalidArgumentException e) {
                e.printStackTrace();
            }
        });
        peers.put(org2.getName(), peers2);

        try {
            channelTmp.initialize();
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(0);
        }

        this.hfclient = hfclientTmp;
        this.channel = channelTmp;
        this.user = userTmp;
    }
}
